package controllers;

import java.text.SimpleDateFormat;
import java.util.Date;

import models.TimeLog;
import play.*;
import play.data.DynamicForm;
import play.data.Form;
import play.mvc.*;

import views.html.*;

@Security.Authenticated(Secured.class)
public class Application extends Controller {

    public static Result index() {
		return ok(timelog.render(session("connected"),Form.form(TimeLog.class)));
    }

    public static Result submitTimecard(){
    	DynamicForm requestData = Form.form().bindFromRequest();
//		Form<TimeLog> timelogForm = Form.form(TimeLog.class).bindFromRequest();
		TimeLog timeLog = new TimeLog(); 
		timeLog.name=session("connected");
		timeLog.startWorkTime=requestData.get("startworktime");
		timeLog.endWorkTime=requestData.get("endworktime");
		timeLog.startBreakTime=requestData.get("startbreaktime");
		timeLog.endBreakTime=requestData.get("endbreaktime");
		timeLog.startOtTime=requestData.get("startottime");
		timeLog.endOtTime=requestData.get("endottime");
		timeLog.PTO=Integer.valueOf(requestData.get("PTO"));
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		timeLog.date=sdf.format(date);
		
		TimeLog.create(timeLog);

//		return ok(timelog.render(session("connected"),Form.form(TimeLog.class)));
		
		return ok(report.render(session("connected"),"Submitted successfully! Thanks for your support."));
//    	return ok(index.render("Submitted successfully! Will back to Timelog page after 3 secs. Thanks for your support."));
    }
    
    public static Result report(){
    	return ok(report.render(session("connected"),"Thanks for your support."));
    }
    
    public static Result delete(Long id){
    	TimeLog.remove(id);
    	return ok(report.render(session("connected"),"Record deleted!"));
    }
    
}
